package com.mike.bdb.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import com.mike.bdb.util.DataCurrent;
import com.mike.bdb.util.ImageManager;
import com.mike.swt.util.ShellUtil;
import org.eclipse.swt.widgets.Link;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class BugReport {

	private static final Logger LOG = LoggerFactory.getLogger(BugReport.class);
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private Shell motherland;
	protected Shell shell;

	private volatile boolean open = false;
	private Text bugTitle;

	private StyledText	bugContext;
	
	public BugReport(Shell motherland) {
		this.motherland = motherland;
	}
	
	public boolean isOpen() {
		return open;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			BugReport window = new BugReport(null);
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		open = true;
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		open = false;
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.BORDER | SWT.APPLICATION_MODAL);
		shell.setImage(ImageManager.getImage(ImageManager.ICON_BUG));
		shell.setSize(400, 431);
		shell.setText(this.getClass().getSimpleName());
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		{
			{
				{
					Composite composite = new Composite(shell, SWT.NONE);
					composite.setLayout(new GridLayout(4, false));
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					{
						Label label = new Label(composite, SWT.NONE);
						label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
						label.setText("\u6807\u9898\uFF1A");
					}
					{
						bugTitle = new Text(composite, SWT.BORDER);
						bugTitle.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
					}
					{
						Label label = new Label(composite, SWT.NONE);
					}
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					{
						Label label = new Label(composite, SWT.NONE);
						label.setText("\u63CF\u8FF0\uFF1A");
					}
					{
						bugContext = new StyledText(composite, SWT.BORDER);
						bugContext.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
					}
					{
						Label label = new Label(composite, SWT.NONE);
						label.setText("     ");
					}
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					{
						Button btnok = new Button(composite, SWT.NONE);
						btnok.addSelectionListener(new SelectionAdapter() {
							@Override
							public void widgetSelected(SelectionEvent e) {
								final String bugT = bugTitle.getText();
								final String bugC = bugContext.getText();
								if(null == bugT || "".equals(bugT.trim()) || bugT.length() > 1000) return;
								if(null == bugC || "".equals(bugC.trim()) || bugC.length() > 10000) return;
								shell.dispose();
								new Thread(){
									@Override public void run() {
										if(LOG.isErrorEnabled()) {
											LOG.error("\r\n"
													+ sdf.format(new Date()) + "\r\n"
													+ bugT  + "\r\n" 
													+ bugC);
										}
									}
								}.start();
								MessageDialog.openInformation(motherland, "报告问题",
										"问题已经反馈，谢谢您的支持!");
							}
						});
						btnok.setText("\u786E\u5B9A(&Ok)");
						btnok.setImage(ImageManager.getImage(ImageManager.ICON_ACCEPT));
					}
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					new Label(composite, SWT.NONE);
					{
						Label label = new Label(composite, SWT.NONE);
					}
					new Label(composite, SWT.NONE);
				}
			}
		}
		if (null != motherland) {
			ShellUtil.setCenter(shell, motherland);
		}
	}
}
